package com.securities.stock.service.job;


import com.securities.stock.dao.StockDao;
import com.securities.stock.entity.dto.StockDto;
import com.securities.stock.entity.pojo.Stock;
import com.securities.stock.enums.StationEnum;
import com.securities.stock.mapper.StockMapper;
import com.securities.stock.service.crawler.CrawlerService;
import com.securities.stock.utils.SSLHttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

public class CrawlerJob {


    private final static Logger LOGGER = LoggerFactory.getLogger(CrawlerJob.class);


    @Autowired
    private CrawlerService crawlerService;

    @Autowired
    private StockDao stockDao;

    @Autowired
    private StockMapper stockMapper;


    /***
     * 600开头的 是上证A股,属于大盘股,其中6006开头的是最早上市的 6016开头的股票为大盘蓝筹股
     * 000开头的股票是深证A股,001、002开头的股票也都属于深证A股,其中002开头的股票是深证A股中小企业股票;
     * 300开头的股票是创业板股票;
     */

    public  void  updateStcokListByShPrefix600()throws Exception{
        String shCodePrefix1="600";
        for(int i=100;i<999;i++){
            String code=shCodePrefix1+i;
            createStockByCrawl(StationEnum.SH.getCode(), code);
        }
    }
    public  void  updateStcokListByShPrefix601()throws Exception{
        String shCodePrefix1="601";
        for(int i=100;i<999;i++){
            String code=shCodePrefix1+i;
            createStockByCrawl(StationEnum.SH.getCode(), code);
        }
    }
    public  void  updateStcokListByShPrefix603()throws Exception{
        String shCodePrefix1="603";
        for(int i=100;i<999;i++){
            String code=shCodePrefix1+i;
            createStockByCrawl(StationEnum.SH.getCode(), code);
        }
    }
    public  void  updateStcokListByShPrefix605()throws Exception{
        String shCodePrefix1="605";
        for(int i=100;i<999;i++){
            String code=shCodePrefix1+i;
            createStockByCrawl(StationEnum.SH.getCode(), code);
        }
    }
    public  void  updateStcokListBySzPrefix000()throws Exception{
        String shCodePrefix1="000";
        for(int i=100;i<999;i++){
            String code=shCodePrefix1+i;
            createStockByCrawl(StationEnum.SZ.getCode(), code);
        }
    }
    public  void  updateStcokListBySzPrefix001()throws Exception{
        String shCodePrefix1="001";
        for(int i=100;i<999;i++){
            String code=shCodePrefix1+i;
            createStockByCrawl(StationEnum.SZ.getCode(), code);
        }
    }
    public  void  updateStcokListBySzPrefix002()throws Exception{
        String shCodePrefix1="002";
        for(int i=100;i<999;i++){
            String code=shCodePrefix1+i;
            createStockByCrawl(StationEnum.SZ.getCode(), code);
        }
    }


    public void  createStockByCrawl(String station,String code)throws Exception {
        Stock stock=new Stock();
        stock.setCode(code);
        int count= stockMapper.selectCount(stock);
        if(count>0){
            return;
        }
        String crawlUrl="http://hq.sinajs.cn/list="+station+code;
        LOGGER.info("执行get 请求,{}",crawlUrl);
        String crawData= SSLHttpClientUtil.get(crawlUrl);
        LOGGER.info("返回结果为:{}",crawData);
        if(!StringUtils.isEmpty(crawData)){
            int startIndex = crawData.indexOf("\"");
            int endIndex = crawData.lastIndexOf("\"");
            if (endIndex-startIndex<2) {
                return ;
            }
            String stockData = crawData.substring(startIndex + 1, crawData.length() - 4);
            String[] stockColumns = stockData.split(",");
            String name=   stockColumns[0];
            if(name!=null){
                stock.setCode(code);
                stock.setName(name);
                StationEnum stationEnum=StationEnum.getStation(station);
                stock.setStation(stationEnum.getCode());
                stock.setPlate(stationEnum.getName());
                stock.setEquityTotal(new BigDecimal(-1));
                stock.setEquityCirculate(new BigDecimal(-1));
                stock.setType(1);
                stock.setIsValid(-1);
                stock.setSynStatus(-1);
                stock.setUpdated(new Date());
                stock.setCreated(stock.getUpdated());
                stockMapper.insertSelective(stock);
            }
        }
    }

    /***
     * 根据总股本和流通股的最新信息 和所属行业和概念
     * @throws Exception
     */
    public void synEquityAndAttribute()throws Exception{
        Stock param=new Stock();
        param.setSynStatus(-1);
        param.setType(1);
        Stock s= stockDao.selectOneByInfo(param);
        if(s==null){
            LOGGER.info("没有满足条件的对象");
            return;
        }
        Stock gb= crawlerService.crawlerEquity(s);
        gb.setSynStatus(1);
        gb.setUpdated(new Date());
        //如果总股本和流通股本都不为空 才参与数据抓取
        if(gb.getEquityTotal()!=null&&gb.getEquityCirculate()!=null){
            gb.setIsValid(1);
        }
        stockDao.updateByPrimaryKeySelective(gb);
        //跟新 所属行业 和概念
        StockDto dto= crawlerService.crawlerConceptBySina(s.getCode());
        dto.setId(s.getId());
        dto.setCode(s.getCode());
        dto.setName(s.getName());
        stockDao.updateStockIndustryAndConcepts(dto);
    }

    public void resetSynStatus(){
        stockDao.resetAllSynStatus();
    }

    /***
     * 保持数据库活跃
     */
    public void keepActive() {
        stockMapper.selectCount(null);
    }

}
